<?php
namespace Home\Controller;
use Think\Controller;
use Org\Com;
use Org\Com\Message;
class ClientController extends  PublicController
{
    private $pagecount=20;
    private $common=array("update","baimingdan","baiquit","heiquit");
    /**
     * 检测权限
     */
    private function checkright($isajax=false){
        $power=CONTROLLER_NAME."-".ACTION_NAME;
        $info=$_SESSION['my_info'];
        $bigcategroy=$info["bigcate"];
        $smallcategroy=explode(",", $info["smallcate"]);
        if(in_array(ACTION_NAME,$this->common)){
            if(!in_array($power, $smallcategroy)){
                if($isajax){
                    Message::Outofmessagenodata(99, "您没有权限");
                }else{
                    $this->error("没有权限");
                    exit();
                }
            }
        }
    }
    /**
     * status|| 0 黑名单用户  1正常  2 白名单
     */
    public function index(){
        $model=M("client");
        $count=$model->where(array("status"=>1))->count();
        $Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=$model->where(array("status"=>1))->limit($Page->firstRow.','.$Page->listRows)->order("time desc")->select();
        foreach($list as $k => $v){
        	$ip = $v['ip'];
        	$dz = $this->GetIpLookup($ip);
        	$list[$k]['city'] =$dz['city'];
        }
        $this->assign("list",$list);
        $this->assign("page",$show);
        $this->display();
    }
    
    /**
     * 拉黑
     */
    public function update(){
        $this->checkright();
    	if(IS_GET){
    		$id=I("get.id");
			$data['status'] = '0';
    		$detail=M("client")->where(array("uid"=>intval($id)))->save($data);
    		if($detail){
			   $this->success('拉黑成功！',U('Client/hei'));
			}else{
			   $this->error('拉黑失败！',U('Client/index'));
			}
    	}
    }
    //黑名单列表
    public function hei(){
        $model=M("client");
        $count=$model->where(array("status"=>0))->count();
        $Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=$model->where(array("status"=>0))->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k => $v){
        	$ip = $v['ip'];
        	$dz = $this->GetIpLookup($ip);
        	$list[$k]['ip'] =$dz['ip'].$dz['city'];
        }
        $this->assign("list",$list);
        $this->assign("page",$show);
        $this->display();
    }
    
    /**
     * 白名单
     */
    public function bai(){
        $model=M("client");
        $count=$model->where(array("status"=>2))->count();
        $Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=$model->where(array("status"=>2))->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k => $v){
            $ip = $v['ip'];
            $dz = $this->GetIpLookup($ip);
            $list[$k]['ip'] =$dz['ip'].$dz['city'];
        }
        $this->assign("list",$list);
        $this->assign("page",$show);
        $this->display();
    }
    
    //意见反馈
    public function yijian(){
    	$model=M("yijian");
    	$count=$model->where(array('md_client.status'=>1))->join('md_client ON md_yijian.uid = md_client.uid')->count();
    	$Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    	$show       = $Page->show();// 分页显示输出
    	$list=$model->where(array('md_client.status'=>1))->join('md_client ON md_yijian.uid = md_client.uid')->limit($Page->firstRow.','.$Page->listRows)->select();
    	
    	$this->assign("list",$list);
    	$this->assign("page",$show);
    	$this->display();
    	$this->display();
    }
    /**
     * 积分明细
     */
    public function getscore(){
        $openid=I("post.openid");
        $shiwanlist=M("note")->field("userid,cash,times")->where(array("userid"=>$openid))->select();//试玩积分
        $yaoqinglist=M("yaoqing")->field("openid,score,times")->where(array("shangxianopenid"=>$openid))->select();
        $i=0;
        $jifen=array();
        if(is_array($shiwanlist) && count($shiwanlist)){
            foreach ($shiwanlist as $my){
               $jifen[$i]["openid"]=$my["userid"];
               $jifen[$i]["score"]=$my["cash"];
               $jifen[$i]["times"]=$my["times"];
               $jifen[$i]["scoretype"]="试玩";
               $i++;
            }
        }
        if(is_array($yaoqinglist) && count($yaoqinglist)){
            foreach ($yaoqinglist as $yo){
                $jifen[$i]=$yo;
                $jifen[$i]["scoretype"]="邀请";
                $i++;
            }
        }
        $count=count($jifen);
        if($count>0){
           Message::Outmessage(0, $count,$jifen, "查找成功！");
         }else{
           Message::Outofmessagenodata(1, "积分为空");
        }
    }
    
    /**
     * 设置白名单 status：0 黑名单  1正常  2白名单
     */
    public function baimingdan(){
        $this->checkright(true);
        $openid=I("post.openid");
        if(M("client")->where(array("openid"=>$openid))->save(array("status"=>2))){
            Message::Outofmessagenodata(0, "设置成功");
        }else{
            Message::Outofmessagenodata(1, "设置失败");
        }
    }
    
    /**
     * 取消白名单 status：0 黑名单  1正常  2白名单
     */
    public function baiquit(){
        $this->checkright(true);
        $openid=I("post.openid");
        if(M("client")->where(array("openid"=>$openid))->save(array("status"=>1))){
            Message::Outofmessagenodata(0, "设置成功");
        }else{
            Message::Outofmessagenodata(1, "设置失败");
        }
    }
    
    /**
     * 取消黑名单 status：0 黑名单  1正常  2白名单
     */
    public function heiquit(){
        $this->checkright(true);
        $openid=I("post.openid");
        if(M("client")->where(array("openid"=>$openid))->save(array("status"=>1))){
            Message::Outofmessagenodata(0, "设置成功");
        }else{
            Message::Outofmessagenodata(1, "设置失败");
        }
    }
	
}


?>